package com.nulstudio.sppm.entity;

import org.jetbrains.annotations.NotNull;
import org.springframework.security.core.GrantedAuthority;

import java.util.Objects;

/**
 * An implementation of {@code GrantedAuthority}, with an authority string and a description string.
 * Immutable and comparable, compare by alphabetical order of authority string.
 * @author nullsty
 * @version 2.0
 */
public final class NulAuthority implements GrantedAuthority, Comparable<NulAuthority> {

    /**
     * ID of the authority.
     */
    private int id;

    /**
     * Name of the authority, must be unique.
     */
    private String authority;

    /**
     * Description of the authority.
     */
    private String description;

    /**
     * Create a new {@code NulAuthority}, all fields are initialized with {@code null}.
     */
    public NulAuthority() {
        id = 0;
        authority = null;
        description = null;
    }

    /**
     * Create a new {@code NulAuthority}, with full initialization.
     * @param id ID of the authority
     * @param authority authority string
     * @param description description of the authority
     */
    public NulAuthority(int id, String authority, String description) {
        this.id = id;
        this.authority = authority;
        this.description = description;
    }

    /**
     * Get ID of the authority.
     * @return ID of the authority
     */
    public int getId() {
        return id;
    }

    /**
     * Get authority string of the object.
     * @return authority string of the object
     */
    @Override
    public String getAuthority() {
        return authority;
    }

    /**
     * Get description of the authority.
     * @return description of the authority
     */
    public String getDescription() {
        return description;
    }

    private void setId(int id) {
        this.id = id;
    }

    private void setAuthority(String authority) {
        this.authority = authority;
    }

    private void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final NulAuthority that = (NulAuthority) o;
        return Objects.equals(authority, that.authority);
    }

    @Override
    public int hashCode() {
        return Objects.hash(authority);
    }

    @Override
    public int compareTo(@NotNull NulAuthority o) {
        return authority.compareTo(o.authority);
    }
}
